package y24.m5.file;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class MergeAccount {
    private File file1;
    private File file2;
    private File file3;

    public MergeAccount(File file1, File file2, File file3) {
        this.file1 = file1;
        this.file2 = file2;
        this.file3 = file3;
    }
    public Boolean merge(){
        try (FileWriter fileWriter = new FileWriter(file3)) {
            mergeFile(fileWriter, file1);
            mergeFile(fileWriter, file2);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    private static void mergeFile(FileWriter fileWriter, File file) throws IOException {
        try (FileReader fileReader = new FileReader(file)) {
            Scanner scanner = new Scanner(fileReader);
            while (scanner.hasNextLine()) {
                fileWriter.write(scanner.nextLine() + "\n");
            }
        }
    }
}
